package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.bean.DictFieldItem;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.query.DictFielItemdQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2015/7/8.
 */
@Service
public class DictFieldItemService extends BaseServiceImpl<DictFieldItem> {

    @Autowired
    SystemLogService systemLogService;

    @Autowired
    private DictFieldService dictFieldService;
    public void save(DictFieldItem dictFieldItem) {
        Integer currTime = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        dictFieldItem.setLastModifyTime(currTime);
        SystemLog.OperateTypeEnum operateTypeEnum = SystemLog.OperateTypeEnum.ADD;
        if(dictFieldItem.getId()==null){
            dictFieldItem.setCreateTime(DateUtil.getCurrentTime());
            dictFieldItem.setCreateAdminId(WebContext.getCachedUserId());
            dictFieldItem.setCreateAdminName(WebContext.getCachedUser().getName());
            operateTypeEnum = SystemLog.OperateTypeEnum.ADD;
        }else{
            DictFieldItem domain = get(dictFieldItem.getId());
            dictFieldItem.setCreateAdminId(domain.getCreateAdminId());
            dictFieldItem.setCreateTime(domain.getCreateTime());
            dictFieldItem.setCreateAdminName(domain.getCreateAdminName());

            dictFieldItem.setModifyAdminId(WebContext.getCachedUserId());
            dictFieldItem.setModifyAdminName(WebContext.getCachedUser().getName());
            operateTypeEnum = SystemLog.OperateTypeEnum.EDIT;
        }

        this.getDAO().save(dictFieldItem);
        systemLogService.log(SystemLog.ModuleTypeEnum.DICTFIELDITEMLIST, operateTypeEnum, dictFieldItem.getId());

    }
    public DictFieldItem view(Integer id) {
        return get(id);
    }
//    /***
//     * 根据fieldId获取items
//     * @param fieldId
//     * @return
//     */
//    public List<DictFieldItem> listItemsByDictId(Integer fieldId){
//        Query<DictFieldItem> query = Query.build(DictFieldItem.class);
//        query.addEq(DictFieldItem.COLUMN_FIELD_ID,fieldId);
//        query.addOrder(DictFieldItem.COLUMN_SORTINDEX, Query.DBOrder.ASC);
//        return findByQuery(query);
//    }

    /***
     * 根据fieldId获取items
     * @param fieldName
     * @return
     */
    public List<DictFieldItem> listItemsByDictName(String fieldName){
        DictField dictField = dictFieldService.getDictFieldByName(fieldName);
        Query<DictFieldItem> query = Query.build(DictFieldItem.class);
        query.addEq(DictFieldItem.COLUMN_FIELD_ID,dictField.getId());
        query.addOrder(DictFieldItem.COLUMN_SORTINDEX, Query.DBOrder.ASC);
        return findByQuery(query);
    }

    public Paged<DictFieldItem> list(DictFielItemdQuery dictFieldItemQuery) {
        Query query = Query.build(DictFieldItem.class);
        query.addEq("fieldId",dictFieldItemQuery.getFieldId());
        query.addLike("itemKey",dictFieldItemQuery.getItemKey());
        query.addLike("itemTitle",dictFieldItemQuery.getItemTitle());
        query.setPaged(dictFieldItemQuery.getPageNo(),dictFieldItemQuery.getPageSize());
        query.addOrder("id", Query.DBOrder.DESC);
        return findPagedByQuery(query);
    }
    public String getItemKeyByValue(String fieldName,Integer value){
        DictField dictField = dictFieldService.getDictFieldByName(fieldName);
        Query<DictFieldItem> query = Query.build(DictFieldItem.class);
        query.addEq(DictFieldItem.COLUMN_FIELD_ID,dictField.getId());
        query.addEq(DictFieldItem.COLUMN_ITEMVALUE,value);
        DictFieldItem item = get(query);
        if(item!=null)return item.getItemTitle();
        return "";
    }


    public List<DictField> getDictFieldList() {
        return dictFieldService.getDictFieldList();
    }

    public boolean check(Integer id,Integer fieldId,String itemKey,String itemValue,String itemTitle) {
        Query query = Query.build(DictFieldItem.class);
        query.addEq("fieldId",fieldId);
        query.addEq("itemKey",itemKey);
        query.addEq("itemValue",itemValue);
        query.addEq("itemTitle",itemTitle);
        return isExist(query,id);
    }
}
